package com.xy.frame.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Maps;
import com.xy.commons.PageUtil;
import com.xy.frame.dao.UserMapper;

/**
 * Created by Administrator on 2014/9/22.
 */
@Service
public class UserService {
	@Autowired
	UserMapper userMapper;

	/**
	 * 查询用户列表
	 * @param params
	 * @return
	 */
	public List<Map<String, Object>> queryUserList(Map<String, Object> params) {
		return userMapper.queryUserList(params);
	}

	/**
	 * 分页查询用户列表
	 * @param params
	 * @return
	 */
	public Map<String, Object> queryUserListByPage(Map<String, Object> params) {
		//
		List<Map<String, Object>> items = userMapper.queryUserList(params);
		int count = userMapper.queryUserCount(params);
		Map<String, Object> pages = Maps.newHashMap();
		pages.put(PageUtil.ITEMS, items);
		pages.put(PageUtil.TOTALS, count);

		return pages;
	}

	/**
	 * @param loginNum
	 * @return
	 */
	public Map<String, Object> getUserByLoginName(String loginNum) {
		Map<String, Object> params = Maps.newHashMap();
		params.put("loginNum",loginNum);
		return queryUserList(params).get(0);
	}
 
	
	 /**
	 * @param loginNum
	 * @throws Exception
	 */
	@Transactional(rollbackFor=Exception.class)
	public void deleteUser(String loginNum) throws Exception {
		 userMapper.deleteUser(loginNum);
	}
	 
	    /**
	     * @param userMap
	     * @throws Exception
	     */
	@Transactional(rollbackFor=Exception.class)
	public void addUserOrUpdate(Map<String, Object>  userMap) throws Exception {
			if("1".equals(userMap.get("type").toString())){
				userMapper.addUser(userMap);
			}else{
				userMapper.updateUser(userMap);
			}
	}
}
